package com.uwlaser.utils;

import com.uwlaser.socket.client.BaseNettyMVBoxClient;
import com.uwlaser.socket.client.SocketConnectMap;
import com.uwlaser.socket.exception.TcpSocketCodeErrorException;
import com.uwlaser.socket.exception.TcpSocketException;
import io.netty.buffer.Unpooled;

/**
 * @Author QZK
 * @Description TODO
 * @Date 2022-12-05-09:49
 * @Version 1.0
 */
public class MyTcpSocketUtil {

    /**
     * 长连接标识
     */
    public static final String LONG_CONNECTION = "0";

    /**
     * 短链接标识
     */
    public static final String SHORT_CONNECTION = "1";

    /**
     * 字符集
     */
    private static final String CHARACTER = "UTF-8";

    /**
     * 发送长链接Socket请求
     * @param host
     * @param port
     * @param bodyParams
     * @return
     * @throws RuntimeException
     */
    public static String sendLongConnectionRequest(String host, Integer port, String bodyParams) throws Exception {
        byte[] bytes = null;
        try {
            bytes = bodyParams.getBytes(CHARACTER);
        } catch (Exception e){
            throw new TcpSocketCodeErrorException(host,port,bodyParams,LONG_CONNECTION,"程序异常：String转Byte失败，【"+CHARACTER+"】！字符串数据【"+bodyParams+"】异常信息："+e.getMessage());
        }
        return sendLongConnectionRequest(host, port, bytes);
    }

    /**
     * 发送短链接Socket请求
     * @param host
     * @param port
     * @param bodyParams
     * @return
     * @throws RuntimeException
     */
    public static String sendShortConnectionRequest(String host, Integer port, String bodyParams) throws Exception {
        byte[] bytes = null;
        try {
            bytes = bodyParams.getBytes(CHARACTER);
        } catch (Exception e){
            throw new TcpSocketCodeErrorException(host,port,bodyParams,SHORT_CONNECTION,"程序异常：String转Byte失败，【"+CHARACTER+"】！字符串数据【"+bodyParams+"】异常信息："+e.getMessage());
        }
        return sendShortConnectionRequest(host, port, bytes);
    }

    /**
     * 发送长链接Socket请求
     * @param host
     * @param port
     * @param bodyParams
     * @return
     * @throws RuntimeException
     */
    public static String sendLongConnectionRequest(String host, Integer port, byte[] bodyParams) throws Exception {

        return sendRequest(host, port, bodyParams,LONG_CONNECTION);
    }

    /**
     * 发送短链接Socket请求
     * @param host
     * @param port
     * @param bodyParams
     * @return
     * @throws RuntimeException
     */
    public static String sendShortConnectionRequest(String host, Integer port,  byte[] bodyParams) throws Exception {

        return sendRequest(host, port, bodyParams,SHORT_CONNECTION);
    }

    /**
     * 发起Socket请求
     * @param host
     * @param port
     * @param bodyParams
     * @param mode
     * @return
     */
    private static String sendRequest(String host, Integer port, byte[] bodyParams,String mode) throws Exception {
        String res = null;
        BaseNettyMVBoxClient baseNettyMVBoxClient = null;

        try {
            if(mode.equals(SHORT_CONNECTION)){ //短连接
                baseNettyMVBoxClient = new BaseNettyMVBoxClient(host, port);
                //发送数据
                res = baseNettyMVBoxClient.sendMsg(Unpooled.wrappedBuffer(bodyParams));
                //关闭连接
                baseNettyMVBoxClient.shutdown();

            } else { //长连接
                String key = host + ":" + port;
                if(SocketConnectMap.contains(key)){
                    baseNettyMVBoxClient = SocketConnectMap.get(key);
                } else {
                    baseNettyMVBoxClient = new BaseNettyMVBoxClient(host, port);
                }
                SocketConnectMap.put(key, baseNettyMVBoxClient);
                //发送数据
                res = baseNettyMVBoxClient.sendMsg(Unpooled.wrappedBuffer(bodyParams));
            }
        } catch (Exception e) {
            //处理异常后抛出
           throw TcpSocketException.handleException(host,port,new String(bodyParams,CHARACTER),mode,e);
        }
        return res;
    }

    public static void main(String[] args) throws Exception {
        String host = "127.0.0.1";
        int port = 8888;

        String s = sendLongConnectionRequest(host, port, "HelloWorld!");
        System.out.println("接收数据："+s);
    }
}
